$(function(){
   $(".xiandai_scroll").picScroll({css: {"width": 660, "height": 437}, config: {"time": 5000, "type": "left", "speed": 600,"button":false}},function(data){
       if(data == 3){
           $(".J_ui_font").hide();
       }else{
           $(".J_ui_font").show();
       }
       $(".J_ui_font ul li").hide();
       $(".J_ui_font ul li").eq(data).show();
   });
   rightlayer.init();
   hover.init();
   showLayer.init();
   $(".xinadai_name_top_bg .get_name").click(function(){
       $(".xiandai_enter_name_box").show();
       $.Jui._showMasks(0.5);
   })
});

var showLayer = {
    init:function(){
        this.obj = $(".xiandai_enter_name_box");
        if(!!$.browser.msie && parseInt($.browser.version) <= 6){
            this.obj.css({
                left:(($.Jui._getpageSize()[0] - parseInt(this.obj.width())) / 2)
            })
        }else{
            this.obj.css({
                left:(($.Jui._getpageSize()[0] - parseInt(this.obj.width())) / 2),
                top:(($.Jui._getpageSize()[1] - parseInt(this.obj.height())) / 2)
            })
        }
        //$.Jui._showMasks(0.5);
        this.bindFun();
    },
    bindFun:function(){
         var that = this,
         obj = $(".xiandai_enter_name_content").find("li").find(".check");
         obj.val("");
         obj.data("t","0");
         obj.focus(function(){
             if(!$(this).data("t")){
                 $(this).val($(this).data("befo")).removeClass("inib");
             }
             if($(this).next().hasClass("ic2") || $(this).next().hasClass("ic")){
                 $(this).next().remove();
             }
         })

         obj.blur(function(){
            that.checking($(this));
         })

         $(".xiandai_enter_name_content .bottom_but_b,.xiandai_enter_name_title .closed").click(function(){
             $(".xiandai_enter_name_box").fadeOut();
             $.Jui._closeMasks();
         })

         $(".xiandai_enter_name_content .bottom_but_a").click(function(){
             for(var i=0; i<obj.length; i++){
                 if(obj.eq(i).data("t") == 0){
                     that.checking(obj.eq(i));
                     return false;
                     break;
                 }
             }
             /*
             var str={child_name:obj.eq(0).val(),family_name:obj.eq(1).val(),phone:obj.eq(2).val(),qq:obj.eq(3).val(),city:obj.eq(4).val(),desc:obj.eq(5).val(),video_url:obj.eq(6).val()}
             $.getJSON("http://together.hupu.com/signup/child_signup",str, function(data) {
                  $(".xiandai_enter_name_box").fadeOut();
                  $.Jui._closeMasks();
             })
             */
              obj.eq(7).val(that.CovertFromXmlTag(obj.eq(7).val()));
             $("#itemForm").submit();
         })
    },
    checking:function(obj){
       var that = this,str = obj.attr("name"),
           regexp=/^.+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-])+/,
            phone = /(^(\d{3,4}-)?\d{7,8})$|(13[0-9]{9})|(15[0-9]{9})|(18[0-9]{9})/,
            rep = /(^\s*)|(\s*$)/g,
            qq = /[^\d]/,
            error = function(s){
                if(!obj.next().hasClass("ic2")){
                    obj.data("befo",obj.val());
                    obj.data("t",0)
                    obj.val(s).addClass("inib");
                    obj.parent().find(".ic1").remove();
                    obj.after('<span class="ic2"></span>');
                }
            },
            pass = function(){
             if(!obj.next().hasClass("ic1")){
               obj.data("t",1)
               obj.after('<span class="ic1"></span>');
             }
            }
        switch(str){
            case "child_name":
                if(obj.val().replace(rep,"").length==0 || that.fontLength(obj.val()) > 10){
                    error("姓名格式错误");
                    return false;
                }else{
                    pass();
                }
                break;
            case "child_age":
                if(obj.val().replace(rep,"").length==0){
                    error("年龄格式错误");
                    return false;
                }else{
                    pass();
                }
                break;
            case "child_height":
                if(obj.val().replace(rep,"").length==0 || that.fontLength(obj.val()) > 10){
                    error("身高格式错误");
                    return false;
                }else{
                    pass();
                }
                break;
            case "family_name":
                if(obj.val().replace(rep,"").length==0 || that.fontLength(obj.val()) > 10){
                    error("姓名格式错误");
                    return false;
                }else{
                    pass();
                }
               break;
            case "phone":
                if(!phone.test(obj.val())){
                   error("电话格式错误");
                   return false;
                }else{
                   pass();
                }
               break;
            case "qq":
                if(qq.test(obj.val()) || obj.val().length < 3){
                   error("QQ号码格式错误");
                   return false;
                }else{
                   pass();
                }
               break;
            case "city":
                if(obj.val().replace(rep,"").length==0){
                   error("请填写城市");
                   return false;
                }else{
                   pass();
                }
               break;
             case "desc":
                if(obj.val().replace(rep,"").length==0){
                   error("请输入文字");
                   return false;
                }else if(that.fontLength(obj.val()) > 120){
                   error("文字过长，请控制60字以内。");
                   return false;
                }else{
                   pass();
                }
               break;
           default:
        }
    },
    fontLength:function(str){
        if(str){
            var iLen = 0;
            for (var i = 0; i < str.length; i++) iLen += str.charAt(i).charCodeAt() > 255 ? 2 : 1;
            return str = Math.abs(Math.floor(iLen));
        }
    },
    CovertFromXmlTag:function (value){
        value = value.replace(/&/g,"&amp;");
        value = value.replace( /</g,"&lt;");
        value = value.replace( />/g,"&gt;");
        value = value.replace(/\"/g, "&quot;");
        value=value.replace(/ /g,"&nbsp;");
        value = value.replace( /<br/g,"&ltbr;");
        value=value.replace(/\r\n/g,"<br>");
        return value;
    }
}

var hover = {
    init:function(){
        this.obj = $(".xiandai_name_list").find("li");
        this.bindFun();
    },
    bindFun:function(){
        var show_a = $(".xiandai_name_img_d"),
            show_b = $(".xiandai_js_bg"),
            show_c = $(".xiandai_name_img_e"),
            s,
            that = this;
        this.obj.hover(function(){
            $(this).addClass("on");

        },function(){
            $(this).removeClass();

        });

        show_b.hover(function(){
            $(this).parent().find(show_a).show();
            $(this).parent().find(show_c).show();
        },function(){
            $(this).parent().find(show_a).hide();
            $(this).parent().find(show_c).hide();
        })

        show_b.click(function(){
            var thats = $(this);
            if(s){
               return false;
            }
            s = true;
            var num = thats.parent().find(".popnumber").html();
            $.getJSON("http://together.hupu.com/signup/child_vote?child_id="+num, function(data) {
                if(data.error != ""){
                    thats.parents("li").tips(data.error);
                    s = false;
                    return false;
                }
                var obj = thats.parent().find(".xiandai_name_img_e").find("p");
                var obj2 = thats.parent().find(show_c).find("s");
                thats.parent().find(show_c).find(".face_imgs").addClass("on");
                obj2.animate({
                    opacity:1,
                    top:0
                },function(){
                    setTimeout(function(){
                        obj2.animate({
                            opacity:0,
                            top:-10
                        },function(){
                            obj2.css("top",10);
                            s = false;
                        })
                    },1000)
                })
                obj.html(parseInt(obj.html())+1+"票");
           })
        })
    }
}

var rightlayer = {
   init:function(){
       this.bindFun();
   },
   bindFun:function(){
      var right = $(".xiandai_layer_right");
      $(window).scroll(function(){
             if($.Jui._getpageScroll() > 500){
                right.fadeIn();
            }else{
                right.fadeOut();
            }
      })
      right.click(function(){
          $('html, body').animate({
               scrollTop:0
           });
      })
   }
};
/*animate*/
(function($){
    $.Jui = $.Jui || {};
    $.extend($.Jui, {
        // version: "1.0",
        _$: function(a, b) {
            a.siblings().removeClass(b);
            a.addClass(b);
        },
        _showMasks:function(a){
            var str = "<div class='body-mask' style='position:absolute; top:0; left:0; width:100%; height:"+$(document).height()+"px; background-color:#000;  z-index:998;'></div>";
            $("body").append(str);
            $(".body-mask").css("opacity",0);
            $(".body-mask").animate({
                "opacity":a?a:"0.8"
            });
        },
        _closeMasks:function(){
           var close = $(".body-mask");
           close.fadeOut(function(){
               close.remove();
           });
        },
        _getpageSize: function() {
            var de = document.documentElement, arrayPageSize,
                    w = window.innerWidth || self.innerWidth || (de && de.clientWidth) || document.body.clientWidth,
                    h = window.innerHeight || self.innerHeight || (de && de.clientHeight) || document.body.clientHeight;
            arrayPageSize = [w, h]
            return arrayPageSize;
        },
        _getpageScroll: function() {
            var yScrolltop;
            if (self.pageYOffset) {
                yScrolltop = self.pageYOffset;
            } else if (document.documentElement && document.documentElement.scrollTop) {
                yScrolltop = document.documentElement.scrollTop;
            } else if (document.body) {
                yScrolltop = document.body.scrollTop;
            }
            return yScrolltop;
        }
    });
    var defaults = {css: {"top": 20}, config: {speed: 800,easing: "swing",time:0}},
       textAnimation = function(a) {
        return this.each(function() {
            var $this = $(this),
                    settings = $.extend({}, defaults, a);
            $this.animate(settings.css, settings.config.speed, settings.config.easing, function() {
                !!settings.callback && settings.callback();
            });
        })
    };
    $.fn.textAnimation = textAnimation;
})(jQuery);

 /*图片切换效果*/
(function($) {
    var picScroll = function() {
        var arg = arguments,
            defaults = [{css: {"width": 490, "height": 170}, config: {"time": 3000, "type": "fade", "speed": 800,"button":false}}];
            //console.log(ssss[0])
        return this.each(function() {
            var $this = $(this),
                    $$ = function(a) {
                return $this.find(a)
            },
            animates = {
                list: 0,
                options: ["top", "left", "fade"],
                init: function(a) {
                    this.ul = $$(".J_ui_post");
                    this.li = $$(".J_ui_post li");
                    this.but = $$(".J_ui_a li");
                    this.arrays = arguments.length > 0 ? arguments : defaults;
                    for (var i = 0; i < this.arrays.length; i++) {
                        switch (typeof this.arrays[i]) {
                            case 'object':
                                $this.find(".J_ui_picSwitch").css(this.arrays[i].css);
                                this.li.css(this.arrays[i].css)
                                break;
                            case 'function':
                                this.returnF = this.arrays[i];
                                break;
                            default:
                        }
                    }
                    if(!this.options[this.arrays[0].config.type] == "undefined"){
                        return false;
                    }
                    this.config();
                    this.bindClick();
                },
                config: function() {
                    var that = this,
                            con = that.arrays[0].config,
                            arr = (con.type == "top" ? ["top"] : ["left"]);
                    if (con.type == "left" || con.type == "top") {
                        if (con.type == "left") {
                            that.ul.addClass("J_ui_postFloat");
                            that.ul.width($this.width() * that.li.length);
                        }
                        if (that.list == that.li.length) {
                            con.type == "top" ? that.li.first().css(arr[0], that.ul.height()) : that.li.first().css(arr[0], that.ul.width());
                            that.callback = function() {
                                that.li.first().css(arr[0], 0);
                                that.ul.css(arr[0], 0);
                                that.list = 0;
                            }
                        }
                        if(that.list == -1){
                            con.type == "top" ? that.li.last().css(arr[0], -that.ul.height()) : that.li.last().css(arr[0], -that.ul.width());
                            that.callback = function() {
                                that.list = that.li.length-1;
                                that.li.last().css(arr[0], 0);
                                con.type == "top" ? that.ul.css(arr[0], -parseInt($this.height()) * that.list):that.ul.css(arr[0], -parseInt($this.width()) * that.list);;
                            }
                        }
                        that.scrollA();
                    }else if (con.type == "fade") {
                        if (!that.ul.hasClass("J_ui_postPost")) {
                            that.ul.addClass("J_ui_postPost")
                        };
                        if (that.list == that.li.length) {
                            that.list = 0;
                        }
                        that.fadeFun();
                    }
                    $.Jui._$(that.but.eq(that.list == that.li.length ? 0 : that.list), "on");
                    that.rerurnFun(that.returns);
                },
                scrollA: function() {
                    var that = this, textCss,
                            con = that.arrays[0].config;
                    clearTimeout(that.t);
                    con.type == "top" ? textCss = {"top": -parseInt($this.height()) * that.list} : textCss = {"left": -parseInt($this.width()) * that.list};
                    that.ul.stop(true).textAnimation({"css": textCss, "config": con, callback: function() {
                            if (that.callback) {
                                that.callback();
                                that.callback = null;
                            }
                        }});
                    that.setTime();
                },
                fadeFun: function() {
                    var that = this;
                    clearTimeout(that.t);
                    that.li.css('opacity', 1)
                    that.li.eq(that.list).siblings().stop(true).fadeOut(that.arrays[0].config.speed);
                    that.li.eq(that.list).fadeIn(that.arrays[0].config.speed);
                    that.setTime();
                },
                bindClick: function() {
                    var that = this;
                    that.but.mousemove(function() {
                        that.list = $(this).index();
                        $.Jui._$($(this), "on");
                        that.config();
                    })
                    if(that.arrays[0].config.button){
                        $$(".J_ui_butPost_a").click(function(){
                            that.list-=1;
                            that.config();
                        })
                        $$(".J_ui_butPost_b").click(function(){
                            that.list+=1;
                            that.config();
                        })
                    }else{
                        $$(".J_ui_butPost_b").remove();
                        $$(".J_ui_butPost_a").remove();
                    }
                },
                rerurnFun: function(a) {
                    this.returnF ? this.returnF(this.list == this.li.length?0:this.list) : "";
                },
                setTime: function(b) {
                    var that = this;
                    that.t = setTimeout(function() {
                        that.list += 1;
                        that.config();
                    }, that.arrays[0].config.time)
                }
            }
            animates.init.apply(animates, arg);
        });
    }
    $.fn.picScroll = picScroll;
})(jQuery);

(function($) {
    var tips = function(a) {
        return this.each(function() {
            var $this = $(this), t,
                    str = '<div class="tips_layer" style="position: absolute; background-color:#ff6600; display:none; z-index:999; color:#fff">\
                  <div class="tips-text"></div>\
                  <div class="diamond"></div>\
              </div>';
            $(str).appendTo(".xiandai_name_list");
            var $tips_text = $(".tips-text"),
                    $tips_layer = $(".tips_layer");
                $tips_text.html(a);
                $tips_layer.css({
                    "top": $this.position().top+120,
                    "left": $this.position().left+70
                }).fadeIn();

                setTimeout(function(){
                    $tips_layer.remove();
                },3000)
                    /*
            $this.hover(function() {
                $tips_text.html(a);
                $tips_layer.css({
                    "top": $this.offset().top - parseInt($this.height()),
                    "left": $this.offset().left
                }).fadeIn();
            }, function() {
                $tips_layer.fadeOut();
            })
*/
            /*
             a.time ? t = setTimeout(function() {
             $tips_layer.stop(true).fadeOut();
             }, a.time) : "";
             */
        })
    }
    $.fn.tips = tips;
})(jQuery);
